/**
 * Created by wangfan on 2017/5/21.
 */

// $(document).ready(function() {
//     $('#houseTable').DataTable();
// } );
var userTypeId = 0;
var user;
var communityId = "";
var editor; // use a global for the submit and return data rendering
function formatStringyyyyMMddTochinese (value){
    if(value.length >=10){
        return value.substring(0, 4) + "年" + value.substring(5, 7) + "月" + value.substring(8, 10)+ "日";
    } else if(value.length == 7){
        return value.substring(0, 4) + "年" + value.substring(5, 7);
    } else {
        return value;
    }
}

$(document).ready(function () {

    var id =getUrlParam("id");

    var isdelete =getUrlParam("delete");
    var backid =getUrlParam("backid");

    if(isdelete==1){

        $.ajax({
            url: '/commis/api/backNotify/remove?id='+backid,
            async: false,
            dataType: 'json',
            success: function (response) {
               if(response.status==0){
                   $.ajax({
                       url: '/commis/api/index/notify',
                       type: "GET",
                       async: false,
                       dataType: 'json',
                       success: function (result) {
                           if (result.status == 0) {
                               countNotify(result);
                           }else{
                               alert(result.msg);
                           }


                       }, error: function (msg) {
                           alert("发生错误");
                       }
                   })
                   console.log("delete success");
               }else {
                   console.log("delete failed");
               }


            }, error: function () {
                console.log("error");
            }
        });


    }
    $.ajax({
        url: '/commis/api/user/currentUser',
        type: "GET",
        async: false,
        dataType: 'json',
        data: {},
        success: function (response) {
            if (response.status == 0) {
                console.log("用户存在");
                user = response.data;
                userTypeId = user.userTypeId;
                var  communtityId=user.community.communtityId;
                if (userTypeId == 4) {
                    communityId = user.communityId;
                }
                $.ajax({
                    url: '/commis/api/image/getImagePathForCom',
                    type: "POST",
                    async: false,
                    dataType: 'json',
                    data: {communtityId:communtityId},
                    success: function (response) {
                        if (response.status == 0) {
                            $("#yinzhang").css({"width":"790px","background":"url(comImg/"+response.data.yinzhangImg+") no-repeat","background-position":"240px 70px"});

                            $("#jwhzrImg").attr('src',"signImg/"+response.data.signImg);
                            // alert(response.data.yinzhangImg);

                        } else if (response.status == 1) {
                            alert("获取图片出错");

                        }
                    }, error: function (msg) {
                        alert("获取图片出错");
                    }
                })
            } else if (response.status == 1) {
                alert("登录用户已经过期，请重新登录");
                window.parent.location.href = "../../login.html";
            }
        }, error: function (msg) {
            alert("登录用户已经过期，请重新登录");
            window.parent.location.href = "../../login.html";
        }
    })
    $.ajax({
        url: '/commis/api/fund/'+id,
        async: false,
        dataType: 'json',
        success: function (response) {
            console.log(response.ppName);
            $("input[name='nian']").val(response.nian)
            $("#sctime").text(formatStringyyyyMMddTochinese(response.registerTime));

            // $("input[name='hao']").val(response.hao)
            $("input[name='hao']").val(response.hao)
            $("input[name='ppName']").val(response.ppName)
            $("input[name='ppDate']").val(response.ppDate)
            $("input[name='ppAdd']").val(response.ppAdd)
            $("input[name='people']").val(response.people)
            $("textarea[name='ppDetail']").val(response.ppDetail)
            $("input[name='amount']").val(response.amount)
            $("input[name='bigAmount']").val(response.bigAmount)
            $("textarea[name='eventsOrg']").val(response.eventsOrg)
            $("textarea[name='budger']").val(response.budger)
            // $("input[name='ppName']").val(response.ppName)
            // $("#ppName").html(response.ppName);
            // $("#ppDate").html(response.ppDate);
            // $("#ppAdd").html(response.ppAdd);
            // $("#people").html(response.people);
            // $("#ppDetail").html(response.ppDetail);
            // $("#amount").html(response.amount);
            // $("#eventsOrg").html(response.eventsOrg);
            // $("#budger").html(response.budger);
            if (userTypeId != 4) {
                $("#audit").show();
            }
            if(response.isOff==7||response.isOff==1){
                $("#audit").show();
            }



        }, error: function () {

        }
    });

    $.ajax({
        url: '/commis/api/image/getImagePathForFund',
        type: "POST",
        async: false,
        dataType: 'json',
        data: {fundId:id},
        success: function (response) {
            if (response.status == 0) {
                var  data =response.data;
                for(var i=0;i<data.length;i++){
                    var item =data[i];
                    if(item.user_type_id==6){
                         $("#guanpianSug").html(item.suggest?item.suggest:"&nbsp;&nbsp;   ");
                         $("#guanpianDate").html(formatStringyyyyMMddTochinese(item.current_datetime));
                        $("#guanpianImg").attr('src',"signImg/"+item.image);

                    }else if(item.user_type_id==7){
                        $("#kezhangSug").html(item.suggest?item.suggest:"&nbsp;&nbsp;  ");
                        $("#kezhangDate").html(formatStringyyyyMMddTochinese(item.current_datetime));
                        $("#kezhangImg").attr('src',"signImg/"+item.image);

                    }else if(item.user_type_id==8){
                        $("#zhuguanSug").html(item.suggest?item.suggest:" &nbsp;&nbsp; ");
                        $("#zhuguanDate").html(formatStringyyyyMMddTochinese(item.current_datetime));
                        $("#zhuguanImg").attr('src',"signImg/"+item.image);


                    }else if(item.user_type_id==9){
                        $("#zhurenSug").html(item.suggest?item.suggest:" &nbsp;&nbsp; ");
                        $("#zhurenDate").html(formatStringyyyyMMddTochinese(item.current_datetime));
                        $("#zhurenImg").attr('src',"signImg/"+item.image);

                    }

                }
                // $("#yinzhang").attr('src',"comImg/"+response.data.yinzhangImg);
                // $("#jwhzrImg").attr('src',"signImg/"+response.data.signImg);

                // alert(response.data.yinzhangImg);

            } else if (response.status == 1) {
                alert("获取图片出错");

            }
        }, error: function (msg) {
            alert("获取图片出错");
        }
    })

    $('#changeTable').DataTable({

        ajax: "/commis/api/fund/changeDatatables?id=" + id,

        autoWidth: true,
        responsive: true,
        serverSide: true,
        processing: true,
        lengthChange: false,//是否允许用户改变表格每页显示的记录数
        ordering: false,//是否允许用户排序
        paging: true,//是否分页
        pagingType: "full_numbers",//除首页、上一页、下一页、末页四个按钮还有页数按钮
        searching: false,//是否开始本地搜索
        stateSave: false,//刷新时是否保存状态
        autoWidth: true,//自动计算宽度
        language: {
            emptyTable: "没有数据",
            search: "搜索:",
            paginate: {
                first: "首页",
                previous: "上一页",
                next: "下一页",
                last: "尾页"

            },
            lengthMenu: "每页 _MENU_ 条记录",
            zeroRecords: "没有找到记录",
            info: "第 _PAGE_ 页 ( 总共 _PAGES_ 页 )，共 _TOTAL_ 项",
            infoEmpty: "无记录",
            infoFiltered: "(从 _MAX_ 条记录过滤)"
        },
        columns: [
            {
                data: 'current_state',
                render: function (data, type, row) {
                    if (0 == data) {
                        return "待审核";
                    } else if (1 == data) {
                        return "同意"
                    }
                    else if (2 == data) {
                        return "不同意";
                    }
                    else if(3 == data)
                    {
                        return "建议";
                    }else if(4 == data)
                    {
                        return "更新";
                    }else if(5 == data)
                    {
                        return "重新提交";
                    }
                }
            }, {
                data: 'real_name', defaultContent: ""
            }, {
                data: 'current_datetime', defaultContent: ""

            }, {
                data: 'suggest', defaultContent: ""

            }, {
                data: 'amount', defaultContent: ""

            },  {
                data: 'submit_number', defaultContent: ""
            }]


    });







})
//设置框架
$(function () {
    $("#page_style").frame({
        float: 'left',
        menu_nav: '.Quick_operation',
        color_btn: '.skin_select',
        Sellerber_menu: '.list_content',
        Sellerber_header: '.Sellerber_header',
    });
});
$("#iframe_box").niceScroll({
    cursorcolor: "#888888",
    cursoropacitymax: 1,
    touchbehavior: false,
    cursorwidth: "5px",
    cursorborder: "0",
    cursorborderradius: "5px"
});
/*********************/
require.config({
    paths: {
        echarts: './js/dist'
    }
});